package com.itheima.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/json")
public class JsonController01 {

//    方式1(objectmapper转化对象为json格式)
@RequestMapping("/test01")
@ResponseBody
public String test01() throws JsonProcessingException {
    User user = new User();
    user.setAge(18);
    ObjectMapper am = new ObjectMapper();
    return am.writeValueAsString(user);
}

//  方式2  spring配置注解驱动 自动转化
@RequestMapping("/test02")
@ResponseBody
public User test02() throws JsonProcessingException {
    User user = new User();
    user.setAge(18);
    return user;
}


@RequestMapping("/test03")
@ResponseBody
public Map test03() throws JsonProcessingException {
        User user = new User();
        user.setAge(18);
        Map m=new HashMap();
        m.put("user",user);

        return m;
    }


}
